;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Internal.Organize", [])
        .controller("OrganizeCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService) {
            $scope.activeFlag = [true, false, false];
            $scope.submitted = false;
            $scope.dataList = [];
            $scope.detailInfo = {};
            $scope.tabFlag = {};
            var tabFlag = {
                0: "新增",
                1: "编辑"
            };
            $scope.root = {
                id: "0",
                isChecked: false,
                children: []
            };
            var editingDepart = {};
            $scope.currentDepart = {};

            var btnPermissionCode = {
                deptAdd: window.ActionCode.deptAdd,
                deptEdit: window.ActionCode.deptEdit,
                deptDel: window.ActionCode.deptDel
            };

            $scope.btnPermission = {
                deptAdd: true,
                deptEdit: true,
                deptDel: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    default:
                        break;
                }
            };

            function initData() {
                $scope.tabFlag = {};
                $scope.detailInfo = {};
            }

            $scope.getChildData = function (parent) {
                if (parent.children.length > 0 && parent.id !== "0") {
                    return;
                }
                var service = "auth";
                var reqUrl = "/department/list";
                var param = {id: parent.id};
                ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value, function (item) {
                        item.isChecked = parent.isChecked;
                        item.isParent = (item.leaf === 0);
                        item.children = [];
                    });
                    parent.children = resp.value;
                })
            };

            $scope.addSubNode = function (item) {
                $scope.currentDepart = item;
                $scope.detailInfo.parentId = item.id;
                $scope.detailInfo.parentNodeName = item.name || "";
                $scope.tabFlag.type = 0;
                $scope.tabFlag.name = tabFlag[0];
                $scope.changeTab(1);
            };

            $scope.delData = function (parentNode, index) {
                var service = "auth";
                var reqUrl = "/department/remove";
                var param = {id: parentNode.children[index].id};
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            var message = resp.message;
                            if (resp.value.length > 0) {
                                message = '部门' + resp.value.join(',') + '存在人员，删除失败';
                            }
                            ybWidget.ShowMessage(message, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        parentNode.children.splice(index, 1);
                        if (parentNode.children.length === 0) {
                            parentNode.isParent = false;
                        }
                    })
                }, function () {

                })
            };

            $scope.editData = function (curDepart, pareDepart) {
                editingDepart = curDepart;
                $scope.tabFlag.type = 1;
                $scope.tabFlag.name = tabFlag[1];
                $scope.detailInfo = {
                    id: curDepart.id,
                    parentNodeName: pareDepart.name || "",
                    departName: curDepart.name
                };
                $scope.changeTab(1);
            };

            $scope.saveData = function (isValid) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                var service = "auth";
                var reqUrl = "/department/insert";
                var param = {};
                if ($scope.tabFlag.type === 1) {
                    reqUrl = "/department/update";
                    param.id = $scope.detailInfo.id;
                }
                param.parentId = $scope.detailInfo.parentId;
                param.name = $scope.detailInfo.departName;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.currentDepart.isParent = true;
                    $scope.currentDepart.isExpand = true;
                    if ($scope.tabFlag.type === 0) {
                        var newDepart = {
                            name: $scope.detailInfo.departName,
                            id: resp.value,
                            leaf: 1,
                            parentId: $scope.currentDepart.id,
                            children: []
                        };
                        $scope.currentDepart.children.push(newDepart);
                    } else {
                        editingDepart.name = $scope.detailInfo.departName;
                    }
                    $scope.changeTab(0);
                });
            };

            (function () {
                $scope.getChildData($scope.root);
                setBtnPermission();
            })();
        }])
    ;
})(window, window.angular);